﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.ArchiveMange.Services;
using CommonModule;
using System.Data;
using ePlanet.ArchiveMange.Models;
using Web.SystemConfig.WorkFlow;
using Web.SystemConfig.Services;
using ePlanet.ArchiveBorrow.Models;
using ePlanet.ArchiveBorrow.Services;

namespace Web.Views.ArchiveBorrow.ArchiveBorrowView
{
    public partial class ArchiveBorrowArchiveHistoryView : PageBase
    {

        #region 私有属性
        protected string archiveTypeName = string.Empty;

        [Dependency]
        public ICodeTable CodeTableService
        {
            get;
            set;
        }

        [Dependency]
        public IArchiveMenuService ArchiveMenuService
        {
            get;
            set;
        }

        [Dependency]
        public IDocumentArchiveVolService DocumentVolService
        {
            get;
            set;
        }

        [Dependency]
        public IDocumentArchiveVolFileService DocumentVolFileService
        {
            get;
            set;
        }

        [Dependency]
        public IZongheVolService ZongheVolService
        {
            get;
            set;
        }

        [Dependency]
        public IZongheVolFileService ZongheVolFileService
        {
            get;
            set;
        }

        [Dependency]
        public IArchiveBorrowService m_IArchiveBorrowService
        {
            get;
            set;
        }

        protected ArchiveBorrowModel.ARCHIVEBORROWRow m_CurrentArchiveBorrowModel = null;

        /// <summary>
        /// 历史档案类型ID
        /// </summary>
        public int ArchiveHistoryType
        {
            get
            {
                if (Request.QueryString["ArchiveType"] != null)
                {
                    return int.Parse(Request.QueryString["ArchiveType"]);
                }
                else
                {
                    throw new Exception("历史档案类型ID不能为空");
                }
            }
        }

        /// <summary>
        /// 档案ID
        /// </summary>
        public Guid ArchiveBorrowID
        {
            get
            {
                if (Request.QueryString["DataID"] != null)
                {
                    return new Guid(Request.QueryString["DataID"]);
                }
                else
                {
                    throw new Exception("档案ID不能为空");
                }
            }
        }

        public string Referer = string.Empty;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    var referer = Request.Headers["Referer"];
                    if (!string.IsNullOrEmpty(referer))
                    {
                        this.Referer = referer;
                    }
                    else
                    {
                        this.Referer = Request.Url.OriginalString;
                    }

                    InitControls();
                    this.InitArchiveBorrowModel(ArchiveBorrowID);
                    LoadData();
                }
                catch (Exception ex)
                {
                    WriteErrorLog(ex.StackTrace);
                    Alert(ex.Message);
                }
            }
        }

        protected void InitArchiveBorrowModel(Guid dataID)
        {
            ArchiveBorrowModel model = this.m_IArchiveBorrowService.GetSingleArchiveBorrowModelByDataID(dataID);
            if (model != null && model.ARCHIVEBORROW.Count > 0)
            {
                this.m_CurrentArchiveBorrowModel = model.ARCHIVEBORROW[0];
            }
            else
            {
                throw new Exception("未查询到相应的借阅信息。");
            }
        }

        /// <summary>
        /// 初始化数据项
        /// </summary>
        private void InitControls()
        {
            //保管期限
            DataSet KeepTime = CodeTableService.GetCodeTableDataByType("KeepTime");
            if (KeepTime != null && KeepTime.Tables.Count > 0)
            {
                this.ddlKeepTime.DataSource = KeepTime;
                this.ddlKeepTime.DataTextField = "Code_Desc";
                this.ddlKeepTime.DataValueField = "CodeTableID";
                this.ddlKeepTime.DataBind();
                this.ddlKeepTime.Items.Insert(0, new ListItem("--请选择--", "-1"));
            }
            DataSet CarrierType = CodeTableService.GetCodeTableDataByType("CarrierType");
            if (CarrierType != null && CarrierType.Tables.Count > 0)
            {
                this.ddlCarrierType.DataSource = CarrierType;
                this.ddlCarrierType.DataTextField = "Code_Desc";
                this.ddlCarrierType.DataValueField = "CodeTableID";
                this.ddlCarrierType.DataBind();
                this.ddlCarrierType.Items.Insert(0, new ListItem("--请选择--", "-1"));
            }

            DataSet SecurityData = CodeTableService.GetCodeTableDataByType("SecurityType");
            if (CarrierType != null && CarrierType.Tables.Count > 0)
            {
                this.ddlSecurity.DataSource = SecurityData;
                this.ddlSecurity.DataTextField = "Code_Desc";
                this.ddlSecurity.DataValueField = "CodeTableID";
                this.ddlSecurity.DataBind();
                this.ddlSecurity.Items.Insert(0, new ListItem("--请选择--", "-1"));
            }
            this.txtArchiveType.Enabled = false;

            switch (ArchiveHistoryType)
            {
                //文书卷101

                case ((int)ArchiveHistoryTypeEnum.DocumentVol):
                    archiveTypeName = "文书卷";
                    break;
                //文书卷内件102
                case ((int)ArchiveHistoryTypeEnum.DocumentVolFile):
                    archiveTypeName = "文书卷内件";
                    break;
                //勘探综合卷
                case ((int)ArchiveHistoryTypeEnum.ZongheVol):
                    archiveTypeName = "勘探综合卷";
                    break;
                //勘探综合卷内件
                case ((int)ArchiveHistoryTypeEnum.ZongheVolFile):
                    archiveTypeName = "勘探综合件";
                    break;
            }
        }

        /// <summary>
        /// 根据页面参数获取页面数据（弱类型DataTable）
        /// </summary>
        /// <returns></returns>
        private DataTable GetArchiveData()
        {
            //根据参数类型加载数据
            DataTable dtArchiveHistory = null;
            switch (ArchiveHistoryType)
            {
                //文书卷101
                case ((int)ArchiveHistoryTypeEnum.DocumentVol):
                    dtArchiveHistory = DocumentVolService.GetAllDocumenArchivetDataById(this.m_CurrentArchiveBorrowModel.ARCHIVEDATAID);
                    break;

                //文书卷内件102
                case ((int)ArchiveHistoryTypeEnum.DocumentVolFile):
                    dtArchiveHistory = DocumentVolFileService.GetAllDocumenArchivetDataById(this.m_CurrentArchiveBorrowModel.ARCHIVEDATAID);
                    break;

                //勘探综合卷
                case ((int)ArchiveHistoryTypeEnum.ZongheVol):
                    dtArchiveHistory = ZongheVolService.GetAllZongheVolDataById(this.m_CurrentArchiveBorrowModel.ARCHIVEDATAID);
                    break;

                //勘探综合卷内件
                case ((int)ArchiveHistoryTypeEnum.ZongheVolFile):
                    dtArchiveHistory = ZongheVolFileService.GetAllZongheVolFileDataById(this.m_CurrentArchiveBorrowModel.ARCHIVEDATAID);
                    break;
            }
            return dtArchiveHistory;
        }

        /// <summary>
        /// 加载页面数据
        /// </summary>
        private void LoadData()
        {
            DataTable dt = GetArchiveData();
            if (dt.Rows.Count > 0)
            {
                this.CurrentPageState = PageState.State_Edit;

                DataRow row = dt.Rows[0];
                this.h_archiveTypeId.Value = row["ARCHIVETYPE"].ToString();
                ArchiveMangeModal.ARCHIVEMENUDataTable dtMenu = ArchiveMenuService.GetArchiveMenuById(row["ARCHIVETYPE"].ToString());
                if (dtMenu.Rows.Count > 0)
                {
                    ArchiveMangeModal.ARCHIVEMENURow drMenu = dtMenu.Rows[0] as ArchiveMangeModal.ARCHIVEMENURow;
                    this.txtArchiveType.Text = drMenu.ARCHIVEMENU;
                }
                else
                {
                    //如果类别号读出错误，则显示默认的类别号
                    //this.txtArchiveType.Text = catalog;
                }
                this.txtArchiveNum.Text = row["ARCHIVENUM"].ToString();
                this.txtAwardType.Text = row["AWARDTYPE"].ToString();
                this.txtCopies.Text = row["COPIES"].ToString();
                this.txtRecordUser.Text = row["RECORDUSER"].ToString();
                if (row["EndTime"] != DBNull.Value)
                {
                    this.txtEndTime.Text = Convert.ToDateTime(row["EndTime"]).FormatToDate();
                }
                else
                {
                    this.txtEndTime.Text = string.Empty;
                }
                this.txtDocumentNum.Text = row["DOCUMENTNUM"].ToString();
                this.txtFileOrg.Text = row["FileOrgID"].ToString();
                if (row["FileTime"] != DBNull.Value)
                {
                    this.txtFileTime.Text = Convert.ToDateTime(row["FileTime"]).FormatToDate();
                }
                else
                {
                    this.txtFileTime.Text = string.Empty;
                }
                this.txtFileUser.Text = row["FileUser"].ToString();
                this.txtPageCou.Text = row["PegeCou"].ToString();
                if (row["RecordTime"] != DBNull.Value)
                {
                    this.txtRecordTime.Text = Convert.ToDateTime(row["RecordTime"]).FormatToDate();
                }
                else
                {
                    this.txtRecordTime.Text = string.Empty;
                }
                this.txtRecordUser.Text = row["RecordUser"].ToString();
                this.txtRemark.Text = row["Remark"].ToString();
                this.txtResponsibleUser.Text = row["RESPONSIBLEUSER"].ToString();
                if (row["StartTime"] != DBNull.Value)
                {
                    this.txtStartTime.Text = Convert.ToDateTime(row["StartTime"]).FormatToDate();
                }
                else
                {
                    this.txtStartTime.Text = string.Empty;
                }
                this.txtSummary.Text = row["Summary"].ToString();
                this.txtTitle.Text = row["Title"].ToString();
                this.txtTiyao.Text = row["Tiyao"].ToString();
                this.ddlCarrierType.SelectedValue = row["F_CARRIERTYPE"].ToString();
                this.ddlKeepTime.SelectedValue = row["F_KEEPTIME"].ToString();
                this.ddlSecurity.SelectedValue = row["F_SECURITYTYPE"].ToString();
            }
        }
    }
}